package com.hnepri.test;

import com.alibaba.fastjson.JSON;
import com.hnepri.fang.ftx2.model.Residential;
import com.hnepri.util.DBUtil;
import com.hnepri.util.FileUtil;
import com.hnepri.util.StringUtil;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.sql.*;
import java.util.List;

public class JSON2CSV {
    static void writeJson2txt() throws Exception{
        byte[] datas = Files.readAllBytes(Paths.get("/Users/zhaoheng/IdeaProjects/fang/doc/xiaoqu.json"));
        String json = new String(datas, "utf-8");
        List<Residential> temp = JSON.parseArray(json,Residential.class);
        System.out.println(temp.size());
        PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("/Users/zhaoheng/data.txt"), "utf-8")));
        pw.println("\"city\",\"citycn\",\"name\",\"district\",\"comarea\",\"type\",\"price\",\"address\",\"domain\"");
        for (Residential o:temp) {
            pw.println(o.getCity() + "|" + o.getCitycn() + "|" + o.getName() + "|" +
                    o.getDistrict() + "|" + o.getComarea() + "|" + o.getType() + "|" +
                    StringUtil.null2empty(o.getPrice()) + "|" + o.getAddress() + "|" + o.getDomain());
        }
        pw.close();
    }
    static void write2db() throws Exception{
        List<String> lines = FileUtil.getLineList("/Users/zhaoheng/data.txt", "utf-8");
        Connection con = null;
        PreparedStatement ps = null;
        try {
            con = DBUtil.getConnection();
            con.setAutoCommit(false);
            String sql = "insert into xiaoqu(city,citycn,name,district,comarea,type,price,address,domain)" +
                    "values(?,?,?,?,?,?,?,?,?)";
            ps = con.prepareStatement(sql);

            int i = 0;
            for(String line:lines) {
                if (line == null || line.trim().equals(""))continue;
                i++;
                String [] datas = line.split("\\|");
                ps.setString(1, datas[0]);
                ps.setString(2, datas[1]);
                ps.setString(3, datas[2]);
                ps.setString(4, datas[3]);
                ps.setString(5, datas[4]);
                ps.setString(6, datas[5]);
                String price = StringUtil.null2empty(datas[6]);
                if (price.equals("")) {
                    ps.setNull(7, Types.NULL);
                } else {
                    ps.setDouble(7, Double.valueOf(price));
                }

                ps.setString(8, datas[7].trim());
                ps.setString(9, datas[8].trim());

                ps.addBatch();
                if (i % 100 == 0) {
                    ps.executeBatch();
                }
            }
            ps.executeBatch();

            con.commit();
        } catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }finally {
            try {
                DBUtil.closeResource(con, ps);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    static void writeDataLen() {
        List ls = FileUtil.getLineList("/Users/zhaoheng/data.txt", "utf-8");
        System.out.println(ls.size());
    }
    public static void main(String[] args) throws IOException {
        try {
            writeDataLen();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
